{extend name="common" /}

{block name='content'}
<div class="layuimini-container">
    <blockquote class="layui-elem-quote">
        <b>注意</b>：<br/>定时器开始、暂停、重起都是在下一分钟开始生效!!!<br/>启动中的定时任务修改"任务脚本"或"任务频率"后，需要重启才会生效!!!<br/>
        <p id="crontab-status"></p>
    </blockquote>
    <div class="layuimini-main">
        <table id="currentTable" class="layui-table layui-hide" lay-filter="currentTable"
               data-auth-add="1"
               data-auth-edit="1"
               data-auth-delete="1">
        </table>
    </div>
</div>
{/block}

{block name="js"}
<script>
    layui.extend({
        ea: 'lay-module/ea/ea.min'
    }).use(['ea', 'jquery'], function () {
        let ea = layui.ea,
            $ = layui.jquery,
            init = {
                table_elem: '#currentTable',
                table_render_id: 'currentTableRenderId',
                index_url: 'system_crontab/index',
                add_url: 'system_crontab/add',
                edit_url: 'system_crontab/edit',
                delete_url: 'system_crontab/delete',
                export_url: 'system_crontab/export',
                modify_url: 'system_crontab/modify',
                flow_url: 'system_crontab/flow',
                relod_url: 'system_crontab/reload',
                ping_url: 'system_crontab/ping',
            };

        (function () {
            $.ajax({
                url: ea.url(init.ping_url),
                type: 'get',
                contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                dataType: "json",
                data: {},
                timeout: 60000,
                success: function (res) {
                    if (res.code === 1) {
                        $('#crontab-status').html('定时任务 <span class="layui-badge layui-bg-green">运行中</span>');
                    } else {
                        $('#crontab-status').html('定时任务 <span class="layui-badge layui-bg-red">未启动</span> 请在项目根目录执行命令 <b>php crontab.php</b>')
                    }
                },
                error: function (xhr, textstatus, thrown) {
                    console.log('Status:' + xhr.status + '，' + xhr.statusText + '，请稍后再试！');
                }
            });
        })();

        ea.table.render({
            init: init,
            toolbar: ['refresh', 'delete', 'add'],
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {field: 'id', title: 'ID', sort: true, width: 80, search: false},
                {field: 'title', title: '任务标题', edit: "text"},
                {field: 'type', title: '任务类型', selectList: {0: '请求url', 1: '执行sql', 2: '执行shell'}},
                {field: 'frequency', title: '任务频率', edit: "text", search: false},
                {field: 'shell', title: '任务脚本', edit: "text", search: false},
                {field: 'remark', title: '任务备注', edit: "text", search: false},
                {field: 'last_running_time', title: '任务上次执行时间', templet: ea.table.date, search: false},
                {field: 'running_times', title: '任务已执行次数', search: false},
                {field: 'sort', title: '排序', sort: true, edit: 'text', search: false},
                {field: 'status', title: '状态', sort: true, templet: ea.table.switch, selectList: {0: '禁用', 1: '启用'}},
                {field: 'create_time', title: '创建时间', sort: true, templet: ea.table.date, search: 'range'},
                {
                    width: 150, title: '操作', templet: ea.table.tool, operat: [
                        [{
                            text: '重启',
                            url: init.relod_url,
                            field: 'id',
                            method: 'request',
                            title: '确定重启吗？',
                            auth: 'reload',
                            class: 'layui-btn layui-btn-xs layui-btn-success'
                        }, {
                            text: '日志',
                            url: init.flow_url,
                            field: 'id',
                            method: 'open',
                            auth: 'flow',
                            class: 'layui-btn layui-btn-xs layui-btn-normal',
                            extend: 'data-full="false"',
                        }],
                        'delete']
                }
            ]],
        });

        ea.listen();
    });
</script>
{/block}
